//
//  PopCat_GoodsView.m
//  MindBox
//
//  Created by MS on 16-1-11.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "PopCat_GoodsView.h"
#import "Gift_Goods_Cell.h"
#import "PopCat_GoodsCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
@interface PopCat_GoodsView ()
@property (nonatomic,strong) NSMutableArray * itemArray;
@property (weak, nonatomic) IBOutlet UILabel *titleView;

@end

@implementation PopCat_GoodsView


-(void)setPopCatData:(PopCatPage *)popCatData
{
    _popCatData = popCatData;
    
     NSString  *title = [NSString stringWithFormat:@"   %@",popCatData.good_title];
    _titleView.text = title;
    
    int i = 0;
    
    for (PopCat_Goods * goods in popCatData.goods) {
        
        GoodsViewCell * view0 = self.itemArray[i];
        
        [view0.imageView sd_setImageWithURL:[NSURL URLWithString:goods.image_url]];
        view0.titleView.text = goods.short_name;
        
        NSString * price = [NSString stringWithFormat:@"¥ %.2f",[goods.p_price floatValue]];
        view0.priceView.text = price;
        i++;
    }
    
}

+(instancetype)popCatGoodsViewWithXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

-(NSMutableArray *)itemArray
{
    if (_itemArray==nil) {
        _itemArray = [[NSMutableArray alloc] init];
    }
    return _itemArray;
}

-(void)awakeFromNib
{

    [self createOneContentViewWithClass:[Gift_Goods_Cell class] tag:0];
    
    [self createOneContentViewWithClass:[PopCat_GoodsCell class] tag:1];
    [self createOneContentViewWithClass:[PopCat_GoodsCell class] tag:2];
    
    [self createOneContentViewWithClass:[Gift_Goods_Cell class] tag:3];
    
    [self createOneContentViewWithClass:[PopCat_GoodsCell class] tag:4];
    [self createOneContentViewWithClass:[PopCat_GoodsCell class] tag:5];
}

-(void)createOneContentViewWithClass:(Class)ClassName tag:(NSInteger)tag
{
       GoodsViewCell * itemView = [ClassName  goodsViewCllViewWithXib];
    
        [self addSubview:itemView];
        [self.itemArray addObject:itemView];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidCick:)];
        [itemView addGestureRecognizer:tap];
        itemView.tag = tag;
}

-(void)viewDidCick:(UIGestureRecognizer *)tap
{
    if (self.PopCatGoodsBlock) {
        _PopCatGoodsBlock(tap.view.tag);
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 1;
    CGFloat y0 = CGRectGetMaxY(_titleView.frame);
    CGFloat w = (self.width-margin)*0.5;
    CGFloat h0 = (self.height - y0-2*margin)*0.5;
    CGFloat h1 = (self.height - y0-4*margin)*0.25;
    
    int i = 0;
    for (GoodsViewCell * itemView in self.itemArray) {
       //大itemView所在列、行；
        int col0 = i%3;
        int rol0 = i/3;
        
        //小itemView所在行；
        int rol1 = (i+1)/(3-rol0);
        
        if (col0==0) {
            CGFloat y = y0+margin+(margin+h0)*rol0;
            itemView.frame = CGRectMake(0, y, w, h0);
            
        }else{
            
            CGFloat y = y0 + margin +(margin+h1)*rol1;
            itemView.frame = CGRectMake(w+margin, y, w, h1);
        }
        i++;
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
